package com.fzu.normalservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzu.commonutil.entity.AddressBook;
import com.fzu.commonutil.entity.User;
import com.fzu.commonutil.util.*;
import com.fzu.commonutil.entity.ServiceType;
import com.fzu.normalservice.service.ImageService;
import com.fzu.normalservice.service.ServiceTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2023-04-03
 */
@RestController
@RequestMapping("/service-type")
@RequiredArgsConstructor
public class ServiceTypeController {
    private final ServiceTypeService serviceTypeService;
    private final ImageService imageService;

    /**
     * 获取家政服务类型
     * @return
     */
    @GetMapping("/options")
    public R list(ServiceType serviceType) {
        LambdaQueryWrapper<ServiceType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ServiceType::getState, CodeEnum.ENABLE.getCode())
        .orderByAsc(ServiceType::getSeq);
        List<ServiceType> list = serviceTypeService.list(lambdaQueryWrapper);
        for (ServiceType type : list) {
            type.setUrl(imageService.getServiceTypeUrl(type.getId()));
        }
        return R.ok().data(list);
    }

    /**
     * 获取家政服务类型
     * @return
     */
    @GetMapping("/listAll")
    public List<ServiceType> listAll() {
        return serviceTypeService.list(new QueryWrapper<>());
    }

    /**
     * 服务类型分页查询
     * @param serviceType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public R list(ServiceType serviceType, @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                  @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return R.ok().data(serviceTypeService.page(serviceType, pageNum, pageSize));
    }

    /**
     * 增加服务类型
     * @param serviceType
     * @return
     */
    @PostMapping
    public R add(@RequestBody ServiceType serviceType){
        serviceTypeService.save(serviceType);
        return R.ok();
    }

    /**
     * 根据id删除服务类型
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Integer id) {
        serviceTypeService.removeById(id);
        return R.ok();
    }

    /**
     * 更新服务类型信息
     * @param serviceType
     * @return
     */
    @PutMapping
    public R update(@RequestBody ServiceType serviceType) {
        Integer id = serviceType.getId();
        if(id != null){
            serviceTypeService.updateById(serviceType);
        }else {
            throw new ApiException("ID值不能为空");
        }

        return R.ok();
    }


    /**
     * 获取服务类型详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable("id") Integer id) {
        return R.ok().data(serviceTypeService.getById(id));
    }


}

